
cap log close 
clear all
set more off 
set niceness 10
set processor 4
set scheme s1color
gl MAIN = "/Users/Wei/Dropbox"
*gl MAIN = "C:\Users\huang\Dropbox"

gl path1 = "$MAIN/uhs"
gl path2 = "$MAIN/Fertility/Workingdata"
gl path3 = "$MAIN/Fertility/Results"
gl path4 = "$MAIN/Fertility/Results"

gl uhs_path1 = "/Users/Wei/Dropbox/uhs"
gl uhs_path2 =  "/Users/Wei/Dropbox/Wei&Peng/Workingdata" 

log using "$path3/UHS_results.log",replace 

use  "$path2/ocp_uhs_reg", clear 
keep if lninc_per > 7 & lninc_per <12
keep if lnexp_per > 6.5 & lnexp_per <12
keep if lnconsump_per > 6 & lnconsump_per <11.5
drop if saving < -80 
set more off
gen head = head1 == 1 & head2 == 1
gen female_hd = women*head
egen female_hh = max(female_hd), by(dcode hcode prov year)
drop female_hd
*keep if a2 <= 2
egen fine_8_15 = rowmean(fine_age8-fine_age15)
egen fine_16_22 = rowmean(fine_age16-fine_age22)

egen fine_6_20 = rowmean(fine_age6-fine_age20)
egen fine_8_22 = rowmean(fine_age8-fine_age22)
egen fine_8_25 = rowmean(fine_age8-fine_age25)
gen smoke_wine = smoke_share + wine_share

gl FINE_VAR_4 =  "fine_6_15 fine_16_20" 
gl FINE_VAR_1 =  "fine_6_20" 

gl CONTROL_1 = "women#prov##c.year_birth women#year_birth#year" 
gl CONTROL_2 = "women#prov##c.year_birth women#year_birth#year women#prov#year" 
gl HH_STRUCTURE = "women#N_member women##c.(old_prop young_prop married female_prop)"
egen f_p = mean(women), by(dcode hcode year)
keep if age > 25 & age < 60 
keep if year_birth < 1980
g co_old = old_prop > 0 
g co_young = young_prop > 0 

gen house_owner = house_own >= 3 & house_own <= 5 if !mi(house_own)
gen food_exp = food_share - wine_share - sug_share - drink_share - resturant_share
gen sug_drink_smoke_wine = wine_share + sug_share + drink_share 


drop if han_p > 0 & han_p < 1
gen run_wealth_share = run_share + wealth_in_share
gen wealth_estate_share = estate_share + wealth_share
gen gold_beauty = other_gold_share +other_beauty_share
gen food_no_wine = food_share - wine_share
gen cloth_beauty = cloth_share + other_gold + other_beauty
gen sug_drink_wine_rest = sug_drink_smoke_wine+resturant_share


* Table 1, Panel B
preserve 
keep if han_p ==1 
keep if women == 1 
su senior female_hh N_member co_old co_young female_p  lninc_per lnearning_per lnexp_per lnconsump_per saving
restore 


cap erase  "$path3/tab_3_c13.xls" 
cap erase  "$path3/tab_3_c13.txt"
foreach sample in "women == 1" "women == 0"{

foreach FINE_VAR in"$FINE_VAR_1"  {
foreach y_var in "married" "N_member" "co_old" "co_young" "female_p"{
reghdfe `y_var'  women#c.(`FINE_VAR') if han_p ==1 &`sample', a($CONTROL_1) cluster(prov)
su `y_var' if  `sample' 
local mean = r(mean)
outreg2 using "$path3/tab_3_c13.xls",append dec(3) adds(Women, `mean')
}
}
}




cap erase "$path3/tab_4_c14.xls"
cap erase "$path3/tab_4_c14.txt"
foreach sample in "women == 1" "women == 0"{
foreach y_var of var  lninc_per lnearning_per   lnexp_per  lnconsump_per saving{
foreach CONTROL in "$CONTROL_1 "  "$CONTROL_1 $HH_STRUCTURE" {
foreach FINE_VAR in "$FINE_VAR_1"  {
reghdfe `y_var'  women#c.(`FINE_VAR') if han == 1 & `sample', a( `CONTROL') cluster(prov)
su `y_var' if  `sample' 
local mean = r(mean)
outreg2 using "$path3/tab_4_c14.xls",append dec(3) adds(Mean, `mean')
}
}
}
}


cap erase "$path3/tab_5_c15.xls"
cap erase "$path3/tab_5_c15.txt"
foreach y_var of var  cloth_beauty  sug_drink_wine_rest   food_exp {
replace `y_var' =  `y_var'*100 
} 
foreach sample in "women == 1" "women == 0"{
foreach y_var in "female_hh" "cloth_beauty" "sug_drink_wine_rest" "food_exp"{
foreach CONTROL in "$CONTROL_1 " "$CONTROL_1 $HH_STRUCTURE women##c.(lnexp)" {
foreach FINE_VAR in "$FINE_VAR_1"  {
reghdfe  `y_var' women#c.(`FINE_VAR') if han == 1  & `sample' , a(`CONTROL' ) cluster(prov)
su `y_var' if  `sample' 
local mean = r(mean)
outreg2 using "$path3/tab_5_c15.xls",append dec(2) adds(Mean, `mean')
}

}
} 
}
foreach y_var of var  cloth_beauty  sug_drink_wine_rest   food_exp {
replace `y_var' =  `y_var'/100 
} 


cap erase "$path3/tab_c11.xls" 
cap erase "$path3/tab_c11.txt"
foreach y_var of var  labor_share trans_in_share run_wealth_share cons_share trans_out_share so_ins_share  wealth_estate_share{
replace `y_var' =  `y_var'*100 

foreach FINE_VAR in"$FINE_VAR_1"  {
foreach CONTROL in "$CONTROL_1 "  "$CONTROL_1 $HH_STRUCTURE" {
reghdfe `y_var'  women#c.(`FINE_VAR') if han_p == 1 & women == 1, a( `CONTROL' ) cluster(prov)
su `y_var' if  women 
local mean1 = r(mean)
su `y_var' if  men 
local mean2 = r(mean)

outreg2 using "$path3/tab_c11.xls",append dec(2) adds(Women, `mean1', Men, `mean2')
}
}

replace `y_var' = `y_var'/100 
}



cap erase  "$path3/tab_c12.xls"
cap erase  "$path3/tab_c12.txt"
foreach sample in "1" {
foreach CONTROL in "$CONTROL_1 " "$CONTROL_1 $HH_STRUCTURE women##c.(lnexp)" {
foreach FINE_VAR in "$FINE_VAR_1"  {
foreach y_var of var  cloth_beauty  sug_drink_wine_rest   food_exp    long_share  trans_comm_share edu_ent_share house_share  med_share{
replace `y_var' =  `y_var'*100 

reghdfe `y_var' women#c.(`FINE_VAR') if han == 1 & `sample' & women == 1, a(`CONTROL' ) cluster(prov)
su `y_var' if  women 
local mean1 = r(mean)
su `y_var' if  men 
local mean2 = r(mean)
outreg2 using "$path3/tab_c12.xls",append dec(2) adds(Women, `mean1', Men, `mean2')

replace `y_var' = `y_var'/100 
}
} 
}
}

log close 

